package com.leetcode.May;

import java.util.Arrays;

/**
 * @Description: 最长连续序列
 * @Author: Mr.Fxy
 * @CreateTime: 2024-05-10 20:57
 */
public class LongestSequence {
    public static void main(String[] args) {
        int[] nums = new int[]{0, 3, 7, 2, 5, 8, 4, 6, 0, 1};
        System.out.println(longestSequenceMax(nums));
    }

    public static int longestSequenceMax(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }

        Arrays.sort(nums);
        int max = 1;
        int current = 1;

        for (int i = 1; i < nums.length; i++) {
            if (nums[i] == nums[i - 1] + 1) {
                current++;
            } else if (nums[i] != nums[i - 1]) {
                current = 1;
            }
            max = Math.max(max, current);
        }
        return max;
    }

}
